﻿#include "CInlineHook.h"
#include "CIatHook.h"
#include "CHotFixHook.h"
#include "CVehHook.h"
#include "CVehHook2.h"

BOOL WINAPI _CreateProcess(
    _In_opt_ LPCWSTR lpApplicationName,
    _Inout_opt_ LPWSTR lpCommandLine,
    _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes,
    _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
    _In_ BOOL bInheritHandles,
    _In_ DWORD dwCreationFlags,
    _In_opt_ LPVOID lpEnvironment,
    _In_opt_ LPCWSTR lpCurrentDirectory,
    _In_ LPSTARTUPINFOW lpStartupInfo,
    _Out_ LPPROCESS_INFORMATION lpProcessInformation
)
{
    MessageBox(NULL, lpApplicationName, TEXT("Title"), MB_OK);
    return FALSE;
}

void InlineHookCreateProcessW()
{
    ULONG_PTR lpSrcFunc = (ULONG_PTR)GetProcAddress(GetModuleHandleA("kernel32.dll"), "CreateProcessW");
    BOOL bRet = g_inlinehook.InlineHook(lpSrcFunc, (ULONG_PTR)_CreateProcess);
    if (!bRet)
        MessageBox(NULL, TEXT("Hook 失败"), TEXT("Title"), MB_OK);
}

void IatHookCreateProcessW()
{
    ULONG_PTR lpSrcFunc = (ULONG_PTR)GetProcAddress(GetModuleHandleA("kernel32.dll"), "CreateProcessW");
    BOOL bRet = g_IatHook.IatHook("kernel32.dll", "CreateProcessW", lpSrcFunc, (ULONG_PTR)_CreateProcess);
    if (!bRet)
        MessageBox(NULL, TEXT("Hook 失败"), TEXT("Title"), MB_OK);
}

void HotFixHookCreateProcessW()
{
    BOOL bRet = g_HotFixHook.Hook((LPSTR)"kernel32.dll", (LPSTR)"CreateProcessW", (ULONG_PTR)_CreateProcess);
    if (!bRet)
        MessageBox(NULL, TEXT("Hook 失败"), TEXT("Title"), MB_OK);
}

CVehHook* lpVehHook = new CVehHook();
CVehHook2* lpVehHook2 = new CVehHook2();

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    {
        //InlineHookCreateProcessW();
        //IatHookCreateProcessW();
        //HotFixHookCreateProcessW();
        //lpVehHook->AddVehHook((ULONG_PTR)GetProcAddress(GetModuleHandleA("kernel32.dll"), "CreateProcessW"), (ULONG_PTR)_CreateProcess);
        lpVehHook2->AddVehHook2((ULONG_PTR)GetProcAddress(GetModuleHandleA("kernel32.dll"), "CreateProcessW"), (ULONG_PTR)_CreateProcess, GetCurrentProcessId());
        break;
    }
    case DLL_THREAD_ATTACH:
        //lpVehHook2->AddVehHook2((ULONG_PTR)GetProcAddress(GetModuleHandleA("kernel32.dll"), "CreateProcessW"), (ULONG_PTR)_CreateProcess, GetCurrentThreadId());
        break;
    case DLL_THREAD_DETACH:
        break;
    case DLL_PROCESS_DETACH:
        lpVehHook2->Remove((ULONG_PTR)GetProcAddress(GetModuleHandleA("kernel32.dll"), "CreateProcessW"));
        break;
    }
    return TRUE;
}

